//
//  SomeViewController.m
//  No15.3
//
//  Created by Bc_Ltf on 15/1/14.
//  Copyright (c) 2015年 Bc_Ltf. All rights reserved.
//

#import "SomeViewController.h"

@interface SomeViewController ()

@end

@implementation SomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)pressSome:(id)sender {
    if (self.textTextField.text == nil || [self.textTextField.text length] == 0) {
        return;
    }
    
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressString:self.textTextField.text completionHandler:^(NSArray
                                                                         *placemarks, NSError *error) {
        NSLog(@"查询记录数：%i",(int)[placemarks count]);
        
        NSMutableArray* array = [NSMutableArray new];
        
        for (int i = 0; i < [placemarks count]; i++) {
            
            CLPlacemark* placemark = placemarks[i];
            
            CLLocationCoordinate2D coordinate = placemark.location.coordinate;
            NSDictionary* address = placemark.addressDictionary;
            
            MKPlacemark *place = [[MKPlacemark alloc]
                                  initWithCoordinate:coordinate addressDictionary:address];
            
            MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:place];
            
            [array addObject:mapItem];
        }
        
        //关闭键盘
        [self.textTextField resignFirstResponder];
        
        if ([array count] > 0) {
            [MKMapItem openMapsWithItems:array launchOptions:nil];
        }
    }];
}
@end
